﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Entities;
using System.Windows.Media.Imaging;
using Studio77.RuneMaster.Pages;
using Studio77.RuneMaster.Core;

namespace Studio77.RuneMaster.Views
{
    public partial class ToolItem : UserControl
    {
        public ToolType ToolTypeValue { get; set; }
        public bool IsUsedItem { get; set; }
        public int count = 0;
        public bool isSelected;
        public bool isUsed;

        public ToolItem()
        {
            InitializeComponent();
            this.Loaded += ToolItem_Loaded;
            this.MouseLeftButtonDown += ToolItem_MouseLeftButtonDown;
        }

        void ToolItem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickTool);
            if (!IsUsedItem)
            {
                //if (isSelected)
                //{
                //    SetUnselect();
                //}
                //else
                //{
                ToolChoosePage.Instance.SelectTool(ToolTypeValue);
                //}
            }
            else
            {
                //不使用
                ToolChoosePage.Instance.UnUseThisTool(this);
            }
        }

        void ToolItem_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= ToolItem_Loaded;
            SetTool(ToolTypeValue);
        }

        public void SetToolCount(int count)
        {
            this.count = count;
            this.txtCount.Text = count.ToString();
        }

        public void SetTool(ToolType type)
        {
            string uri = string.Empty;
            switch (type)
            {
                case ToolType.Add500:
                    uri = "/Images/add500.png";
                    break;
                case ToolType.CollectSprite:
                    uri = "/Images/CollectSprite.png";
                    break;
                case ToolType.CreateNewSpriteRune:
                    uri = "/Images/AddSpriteRune.png";
                    break;
                case ToolType.ReArrange:
                    uri = "/Images/ReArrange.png";
                    break;
                case ToolType.ScoreMutil2:
                    uri = "/Images/score2.png";
                    break;
                case ToolType.SpriteMutil2:
                    uri = "/Images/Sprite2.png";
                    break;
            }

            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            imgTool.Source = bitmap;
            ToolTypeValue = type;
        }

        internal void SetUnselect()
        {
            if (!IsUsedItem)
            {
                this.selector.Visibility = System.Windows.Visibility.Collapsed;
                isSelected = false;
            }
        }

        internal void SetSelect()
        {
            if (!IsUsedItem)
            {
                this.selector.Visibility = System.Windows.Visibility.Visible;
                isSelected = true;
            }
        }

        internal void SetUsed()
        {
            if (!IsUsedItem)
            {
                isUsed = true;
                this.imgToolSelected.Visibility = System.Windows.Visibility.Visible;
                this.imgTool.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        internal void SetUnUsed()
        {
            if (!IsUsedItem)
            {
                isUsed = false;
                this.imgToolSelected.Visibility = System.Windows.Visibility.Collapsed;
                this.imgTool.Visibility = System.Windows.Visibility.Visible;
            }
        }


    }
}
